package com.dbsop.common.util;

import com.alibaba.fastjson.JSON;
import com.dbsop.domain.info.EsFiledInfo;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * ES 对象处理工具类
 */
public class EsPoJoUtils {

    /**
     * 根据字段名称获取值
     * @param obj
     * @param fieldName
     * @return
     */
    public static String getFieldValue(Object obj,String fieldName){
        if (fieldName == null){
            return null;
        }
        Object idValue = null;
        try {
            Field declaredField = obj.getClass().getDeclaredField(fieldName);
            declaredField.setAccessible(true);
            idValue =  declaredField.get(obj);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return idValue==null?null:idValue.toString();
    }


    /**
     * 将对象转换为ES Map
     * @param t
     * @param fieldMap
     * @param <T>
     * @return
     */
    public static <T> Map<String,Object> getFieldValueMap(T t,Map<String, EsFiledInfo> fieldMap){
        Map<String,Object> map = new HashMap<String, Object>(fieldMap.size());

        fieldMap.forEach((key, esFiledInfo) -> {
            Field field = esFiledInfo.getField();
            field.setAccessible(true);
            try {
                Object value = field.get(t);
                if (value!=null){
                    if ( !TypeOpt.checkEsType(value.getClass())) {
                        value =  JSON.toJSON(value);
                    }
                }
                map.put(key,value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        return map;
    }

    /**
     * 将不为空的字段转换为Map
     * @param t
     * @param fieldMap
     * @param <T>
     * @return
     */
    public static <T> Map<String,Object> getFieldNotNullValueMap(T t,Map<String,EsFiledInfo> fieldMap){
        Map<String,Object> map = new HashMap<String, Object>(fieldMap.size());
        fieldMap.forEach((key, esFiledInfo) -> {
            Field field = esFiledInfo.getField();
            field.setAccessible(true);
            try {
                Object value = field.get(t);
                if (value!=null){
                    if ( !TypeOpt.checkEsType(value.getClass())) {
                        value =  JSON.toJSON(value);
                    }
                }
                if (value == null) return;
                map.put(key,value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return map;
    }

//    public static Map<String,Map<String,Object>> conversEsPoJo(String idFieldName,
//                                                               Map<String,Field> esMapping,Object obj){
//        Map<String,Object> map = new HashMap<String, Object>();
//        String fieldValue = getFieldValue(obj, idFieldName);
//        return null;
//    }
//
//    /**
//     * 将map中为空的字段 进行清理
//     * @param objectMap
//     */
//    public static void mapIsNotNull(Map<String,Object> objectMap){
//        Iterator<Map.Entry<String, Object>> iterator=objectMap.entrySet().iterator();
//        while(iterator.hasNext()){
//            Map.Entry<String, Object> entry=iterator.next();
//            if (entry.getValue() == null){
//                iterator.remove();
//            }
//        }
//    }

}
